<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogin()
    {
        //
        // echo 'hello';die;
        return view('log.login');
    }

    // 登录验证
    public function postCheck(Request $request)
    {
       	// echo 'hello';die;
       	// 查询用户是否存在
       	$user = DB::table('users')->where('username',$request->input('username'))->first();
       	// dd($user);
       	
       	if($user){
       		// 检测密码是否与数据库一致
       		 
       		if(Hash::check($request->input('password'),$user->password)){
       			//dd('hash');
       			//登录成功将用户id放入session
       			session(['id'=>$user->id,'username'=>$user->username]);
            // dd($user->id);
       			return redirect('/admin/index')->with('success','欢迎登录');
       		}else{
       			//dd('error');
       			return back()->with('error','用户名或者密码错误');
       		}
       	}else{
			return back()->with('error','用户名或者密码错误');
       	}
    }


    // 退出登录
    public function logout(){
    	// 清除session中id
    	session()->forget('id');
    	return redirect('/admin/log/login');
    }
}